/*****Log*****/
clear
cd "C:\Users\Joshua\Dropbox\Work\Networks and Economic Perceptions\Data and Do Files"
log using "Figure 1 Output.smcl", replace
	/*the log file is in format .smcl, but can be read via txt editor*/

	
/***********************************************************************
************************************************************************
************************************************************************
This do file contains the needed to replicate our Figure 1. 

For each survey, the code will load and clean the data at hand using the
commands included in the survey cleaning .do files and then do the 
analyses in question. For those wishing to use this to replicate the analyses, 
you will need to update the cd command below to point to where your data
and .do files are located. 

Also note that there are two STATA modules that need to be intalled: 
(1) esttab and (2) grc1leg. 

ssc install estout, replace
ssc install grc1leg, replace

************************************************************************
************************************************************************
***********************************************************************/
	
	
/****************************************
*****************************************
		2000 ANES
*****************************************
****************************************/	


/***Loading and cleaning the data****/
set more off
do "Data Cleaning - 2000 ANES.do"
set more off


/*****************
Analyses: 
Two models are run: (1) one without the interaction and (2) one with. 
After the models we estimate the marginal difference between partisans
for both models. 
*****************/

eststo clear
eststo: logit def_knowl i.partisan c.disagree_total names1 disc_knowl interest i.gender i.race ///
	i.marital age educ income nfc1 evaluate1 [pweight = V000002a]
	
	margins, dydx(partisan)
	margins partisan
	
eststo: logit def_knowl i.partisan##c.disagree_total names1 disc_knowl interest i.gender i.race ///
	i.marital age educ income nfc1 evaluate1 [pweight = V000002a]
		
		margins, dydx(disagree_total) by(partisan)
		summ disagree_total, detail
			*5% = -4; 95% = 2
		margins partisan, at(disagree_total=(-4 2))
		
		margins partisan, at(disagree_total=(-4(1)4)) 
		
		marginsplot, scheme(s1mono) xline(-4, lpattern(dot)) xline(2, lpattern(dot)) title(" " "2000 ANES", size(medium)) ///
			ytitle("Pr(Correct)") xtitle("Disagreement") ylabel(0(0.20)1) ///
			text(0.1 -2.8 "In: b=0.04, z=1.81" ///
			0.01 -2.8 "Out: b=0.03, z=1.33", just(right) size(small))
				
		graph save "2000 - Knowledge", replace


/****************************************
*****************************************
		2008 ANES Panel
*****************************************
****************************************/

/*Data Cleaning*/
clear
do "Data Cleaning - 2008 Panel.do" 
set more off

*May*
rename partisan_9rev partisan
		
eststo: logit def_knowl17 i.partisan c.disagree_total numgiven1 network_interest  interest_w9 i.gender i.race i.marital ///
	age educ income nfc1 evaluate1 [pweight= WGTPP17]
	
	margins, dydx(partisan)
	
	margins partisan
	
eststo: logit def_knowl17 i.partisan##c.disagree_total numgiven1 network_interest interest_w9 i.gender i.race i.marital ///
	age educ income nfc1 evaluate1  [pweight= WGTPP17]
	
	margins, dydx(disagree_total) by(partisan)
	summ disagree_total, detail
	margins partisan, at(disagree_total=(-3 3))
	
	margins partisan, at(disagree_total=(-3(1)3)) 
	
	marginsplot, scheme(s1mono) xline(-3, lpattern(dot)) xline(3, lpattern(dot)) title("ANES 2008-2009:" "May 2009" , size(medium)) ///
		ytitle("Pr(Correct)") xtitle("Disagreement") ylabel(0(0.2)1) ///
		text(0.1 2 "In: b=0.01, z=0.63" ///
		0.01 2 "Out: b=-0.001, z=-0.16", just(right) size(small))
	
	graph save "MAY 2008 - Knowledge (updated)", replace
			
*July*
eststo: logit def_knowl19 i.partisan c.disagree_total numgiven1 network_interest interest_w9 i.gender i.race i.marital age educ income nfc1 evaluate1 [pweight= WGTPP19]
	margins, dydx(partisan) 
	margins partisan

eststo: logit def_knowl19 i.partisan##c.disagree_total numgiven1 network_interest interest_w9 i.gender i.race i.marital age educ income nfc1 evaluate1 [pweight= WGTPP19]
	
	margins, dydx(disagree_total) by(partisan)
	
	margins partisan, at(disagree_total=(-3 3)) 
	
	margins partisan, at(disagree_total=(-3(1)3)) 
	
	marginsplot, scheme(s1mono) xline(-3, lpattern(dot)) xline(3, lpattern(dot)) title("ANES 2008-2009:" "July 2009" , size(medium)) ///
		ytitle("Pr(Correct)") xtitle("Disagreement") ylabel(0(0.2)1) ///
		text(0.1 2 "In: b=0.04, z=2.81" ///
		0.01 2 "Out: b=-0.01, z=-0.63", just(right) size(small))
				
	graph save "JULY KNOWL (updated)", replace
	
	
*July, by May Knowledge*
eststo: logit def_knowl19 i.partisan##c.disagree_total i.def_knowl17 numgiven1 network_interest interest_w9 i.gender i.race i.marital age educ income nfc1 evaluate1 [pweight= WGTPP19]
	margins, dydx(disagree_total) by(partisan) at(def_knowl17=(0))
	
	margins partisan, at(disagree_total=(-3(1)3) def_knowl17=(0))
	

	marginsplot, scheme(s1mono) xline(-3, lpattern(dot)) xline(3, lpattern(dot)) title("July 2009" "Incorrect in May" , size(medium)) ///
		ytitle("Pr(Correct)") xtitle("Disagreement") ylabel(0(0.2)1) ///
		text(0.1 2 "In: b=0.05, z=3.42" ///
		0.01 2 "Out: b=-0.01, z=-0.66", just(right) size(small))
		
	graph save "July Knowl - Incorrect in May (update)", replace
	
	margins, dydx(disagree_total) by(partisan) at(def_knowl17=(1))
	
	margins partisan, at(disagree_total=(-3(1)3) def_knowl17=(1))

	marginsplot, scheme(s1mono) xline(-3, lpattern(dot)) xline(3, lpattern(dot)) title("July 2009" "Correct in May" , size(medium)) ///
		ytitle("Pr(Correct)") xtitle("Disagreement") ylabel(0(0.2)1) ///
		text(0.1 2 "In: b=0.04, z=3.12" ///
		0.01 2 "Out: b=-0.005, z=-0.64", just(right) size(small))
		
	graph save "July Knowl - Correct in May" , replace
	
		

/*Combined Table*/
esttab using TABLE0A6_KNOWLEDGE.rtf, onecell replace label se star(+ 0.10 * 0.05 ** 0.01) nobaselevels ///
			mtitles("2000" "2000" "May 2009" "May 2009" "July 2009" "July 2009"  "July 2009 (W17 Controlled)") ///
			addnotes(Analyses are weighted: V000002a, WGTPP17, WGTPP19) pr2 aic bic ///
			title({\b Table OA6.} "Network Disagreement and Deficit Knowledge")
	
	
/*Figure*/

grc1leg "2000 - Knowledge" "MAY 2008 - Knowledge (updated)" "JULY Knowl (updated)" "July Knowl - Incorrect in May (update)"  , scheme(s1mono)


	/*note: the text boxes were moved out of whack when combining the graphs; 
	they were manually moved via the graph editor and then the graph was saved*/

graph save "Figure 1", replace
graph export "Figure 1.png", replace
graph export "Figure 1.tif", replace


log close
